package cn.wolfcode.crm.util;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemLog;
import cn.wolfcode.crm.service.ISystemLogService;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class SystemLogUtil {

    @Autowired
    private ISystemLogService systemLogService;

    public void writeLog(JoinPoint joinPoint){
        // 隔离 自己 访问自己
        if (joinPoint.getTarget() instanceof ISystemLogService) {
            System.out.println("是自己===============");
            return;
        }


        System.out.println("写入日志");
        // 获取 用户
        Employee user = UserContext.getCurrentUser();
        // 获取 Requeset
        HttpServletRequest req = UserContext.getRequest();
        SystemLog log = new SystemLog();

        log.setOpUser(user);
        log.setOpTime(new Date());


        log.setFunction(joinPoint.getTarget().getClass().getName()+ ":" + joinPoint.getSignature().getName());
        log.setParams(JsonUtil.parseToString(joinPoint.getArgs()));
        log.setOpIp(req.getRemoteAddr());

        systemLogService.saveOrUpdate(log);

    }
}
